package cn.tony.game.super_tank;

import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Tank extends Drawable implements BloodAble {

    private int             speed      = Game.TANK_SPEED;
    private int             cell       = Game.TANK_CELL;

    public DataOutputStream out;
    public DataInputStream  in;

    public int              id;

    public List<Drawable>   zidans     = new ArrayList<>();

    Random                  random     = new Random();

    public NameTitle       name;

    private BloodStrip      bloodStrip = new BloodStrip();

    public Tank(String tankName) {
        super();
        name = new NameTitle(tankName);
        x = y = 30;
        w = h = cell;
        bloodStrip.w = w;
    }

    public Tank(String tankName, int id) {
        super();
        this.id = id;
        name = new NameTitle(tankName);
        x = y = 30;
        w = h = cell;
        bloodStrip.w = w;
    }
    public void setBloodStrip(int cbs) {
        bloodStrip.setCurrentBlood(cbs);
    }

    
    public int gettBloodStrip() {
        return this.bloodStrip.currentBlood;
    }
    public void draw(Graphics g) {
        if (!isAlive) {
            return;
        }

        if (isOverstep()) {
            back();
        } else {
            ai();
        }

        move(dir, speed);

        int tmpDir = checkDir();

        draw(g, tmpDir, MediaUtils.TankImageDir);

        bloodStrip.syncLocation(this);
        bloodStrip.paint(g);

        name.syncLocation(bloodStrip);
        name.paint(g);

        for (int i = zidans.size() - 1; i >= 0; i--) {
            Drawable z = zidans.get(i);

            if (!z.isOverstep()) {
                z.paint(g);
            } else {
                zidans.remove(i);
            }
        }

    }

    public void ai() {
        if (isPlayer)
            return;

        if (random.nextInt(1000) < 35) {
            trigger();
        }

        if (rand()) {
            int ti = random.nextInt(1000) % 8;
            switch (ti) {
            case 0:
                dir = 0b1000;
                break;
            case 1:
                dir = 0b1100;
                break;
            case 2:
                dir = 0b0100;
                break;
            case 3:
                dir = 0b0110;
                break;
            case 4:
                dir = 0b0010;
                break;
            case 5:
                dir = 0b0011;
                break;
            case 6:
                dir = 0b0001;
                break;
            case 7:
                dir = 0b1001;
                break;
            default:
                dir = oDir;
                break;
            }
        }
    }

    private boolean rand() {
        return random.nextInt(100) > 97;
    }

    public void left(boolean b) {
        dir = b ? dir | 0b1000 : dir & 0b0111;
    }

    public void right(boolean b) {
        dir = b ? dir | 0b0010 : dir & 0b1101;
    }

    public void up(boolean b) {
        dir = b ? dir | 0b0100 : dir & 0b1011;
    }

    public void down(boolean b) {
        dir = b ? dir | 0b0001 : dir & 0b1110;
    }

    public Zidan trigger() {
        Zidan zidan = new Zidan();
        zidan.syncLocation(this);
        zidans.add(zidan);
        return zidan;
    }

    public boolean isHit(Drawable d) {
        if (d.isNotCheckHit) {
            return false;
        }

        for (int i = this.zidans.size() - 1; i >= 0; i--) {
            boolean a = this.zidans.get(i).isHit(d);
            if (a) {
                zidans.get(i).isAlive = false;
                if (d instanceof BloodAble) {
                    BloodAble aaaa = (BloodAble) d;
                    aaaa.lose();
                } else {
                    d.isAlive = false;
                }

                return true;
            }
        }

        return false;

    }

    @Override
    public void lose() {
        this.bloodStrip.currentBlood -= 25;
        if (this.bloodStrip.currentBlood <= 0) {
            this.isAlive = false;
        }
    }

}
